import unittest
import requests
import time

class APITest(unittest.TestCase):
    base_url = "http://182.151.48.185:8098/api/private/v1/"

    @classmethod
    def setUpClass(cls):
        token = cls.login(cls)
        cls.header={
            "Authorization":token
        }

    def login(self):
        url = self.base_url + "login"
        mydata = {
            "username": "admin",
            "password": "123456"
        }
        res = requests.post(url=url,data=mydata)
        assert res.status_code == 200
        assert "登录成功" in res.text
        print(res.text)
        token = res.json().get("data").get("token")
        return token

    def test_getusers(self):
        url = self.base_url + "users"

        myparam = {
            "query": "",
            "pagenum": 1,
            "pagesize": 5
        }
        res = requests.get(url,headers=self.header,params=myparam)
        self.assertEqual(200,res.status_code)
        print(res.text)

    def test_adduser(self):
        url = self.base_url + "users"
        now = time.localtime()
        formattime = time.strftime("%Y%m%d%H%M%S",now)
        userinfo = {
            "username":"hkt"+formattime,
            "password":"123456",
            "email":"2278892954@qq.com",
            "mobile":"11112222333"
        }
        res = requests.post(url,json=userinfo,headers=self.header)
        self.assertEqual(200,res.status_code)
        self.assertIn("创建成功",res.text)
        print(res.text)

    def test_searchId(self):
        url = self.base_url + "users/{0}".format(500)
        res = requests.get(url, headers=self.header)
        self.assertEqual(200, res.status_code)
        print(res.text)

if __name__ == '__main__':
    unittest.main()